//
//  HomeShopCartHelper.m
//  FD
//
//  Created by xiaofang wang on 2018/11/21.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  购物车工具类


#import "HomeShopCartHelper.h"
#import "NewShopcartModel.h"

@implementation HomeShopCartHelper

+ (void)shopcartHelperTotalPriceBlock:(void (^)(CGFloat))complection {
    
    //一 调接口请求数据
    //二 计算
    [FDHttpAPi requestSYUserCartListSuccessBlock:^(id responseObjc) {
        //不做处理 直接将服务器返回的数据传过去
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //请求成功
            NSArray *dataArr = responseObjc[@"data"];
            
            NSMutableArray *newArrM = [NSMutableArray array];
            
            //1-1转模型
            for (NSDictionary *dic in dataArr) {
                NewShopcartModel *model = [NewShopcartModel mj_objectWithKeyValues:dic];
                [newArrM addObject:model];
            }
            
            CGFloat sum = 0.0; //总价
            //11-2解析数据计算总价总价
            for (NewShopcartModel *model in newArrM) {
                //单个商品的总价(单价 * 个数)
                CGFloat singlePrice =  model.totalcurrentPrice * [model.quantity floatValue];
                sum += singlePrice;
            }
            
            
            //2019-1-10计算总原价
//            CGFloat originPrice = 0.0;//原价
//            for (NewShopcartModel *model in newArrM) {
//                //单个商品的总价(单价 * 个数)
//                CGFloat singlePrice =  model.totalcurrentPrice * [model.quantity floatValue];
//                sum += singlePrice;
//            }
            
            
            //回调
            if (complection) {
                complection(sum);
            }
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",error);
    }];
}


/**购物车商品数量*/
+ (void)shopcartHelperShopcartListBlock:(void(^)(NSInteger count))complection {
    //一 调接口请求数据
    [FDHttpAPi requestSYUserCartListSuccessBlock:^(id responseObjc) {
        //不做处理 直接将服务器返回的数据传过去
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //请求成功
            NSArray *dataArr = responseObjc[@"data"];
            
            if (complection) {
                complection(dataArr.count);//回调数量
            }
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",error);
    }];
}


/**3.获取运费*/
+ (void)shopcartHelperFreightWithUserShippingAddressId:(NSNumber *)shopAddressId Block:(void (^)(CGFloat))complection {
    
    [FDHttpAPi requestFreightWithUserShippingAddressId:shopAddressId successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"]; //q
        
        if ([event isEqualToString:SUCCESS]) { //提取运费数据
            NSString *price = responseObjc[@"data"][@"price"];
            
            //回调结果
            if (complection) {
                complection([price floatValue]);
            }
            
        }else {
            FDLog(@"请求失败:%@",responseObjc[describe]);
            //toast
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"请求失败:%@",[error localizedDescription]);
    }];
}




/**2019-2-16 用于判断购物车是否为空*/
+ (void)shopcartHelperCarListIsEmptyBlock:(void(^)(BOOL isHaveData))emptyBlock {
    [FDHttpAPi requestSYUserCartListSuccessBlock:^(id responseObjc) {
        //不做处理 直接将服务器返回的数据传过去
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //请求成功
            NSArray *dataArr = responseObjc[@"data"];
       
            BOOL isHave = dataArr.count ? YES : NO;
            
            if (emptyBlock) {
                emptyBlock(isHave);
            }
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",error);
    }];
}
@end

